package com.lineage.server.clientpackets;

import com.lineage.config.ConfigOther;
import com.lineage.echo.ClientExecutor;
import com.lineage.server.Instance.character.CharacterPunishInstance;
import com.lineage.server.Shutdown;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.L1Trade;
import com.lineage.server.serverpackets.S_CloseList;
import com.lineage.server.serverpackets.S_Message_YN;
import com.lineage.server.serverpackets.S_SystemMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.timecontroller.server.ServerRestartTimer;
import com.lineage.server.utils.FaceToFace;
import com.lineage.server.world.World;
import com.lineage.william.L1WilliamSystemMessage;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class C_Trade extends ClientBasePacket {
  private static final Log _log = LogFactory.getLog(C_Trade.class);
  
  public void start(byte[] decrypt, ClientExecutor client) {
    try {
      L1PcInstance pc = client.getActiveChar();
      L1PcInstance player = client.getActiveChar();
      if (ServerRestartTimer.isRtartTime()) {
        pc.sendPackets((ServerBasePacket)new S_SystemMessage("伺服器即将关机 无法交易"));
        return;
      } 
      if (CharacterPunishInstance.get().checkCharacter(Integer.valueOf(pc.getId())).booleanValue())
        return; 
      if (pc.isGhost())
        return; 
      if (pc.isDead() || pc.isTeleport())
        return; 
      L1PcInstance target = FaceToFace.faceToFace(pc);
      if (CharacterPunishInstance.get().checkCharacter(Integer.valueOf(target.getId())).booleanValue())
        return; 
      L1PcInstance srcTrade = (L1PcInstance)World.get().findObject(pc.getTradeID());
      if (srcTrade != null) {
        L1Trade trade = new L1Trade();
        trade.tradeCancel(srcTrade);
        return;
      } 
      if (target.getTradeOk())
        return; 
      if (pc.getTradeOk())
        return; 
      if (target.getLevel() >= 1 && target.getLevel() < ConfigOther.tradelevel) {
        player.sendPackets((ServerBasePacket)new S_SystemMessage(L1WilliamSystemMessage.ShowMessage(10)));
        return;
      } 
      if (pc.getLevel() >= 1 && pc.getLevel() < ConfigOther.tradelevel) {
        pc.sendPackets((ServerBasePacket)new S_SystemMessage(L1WilliamSystemMessage.ShowMessage(11)));
        return;
      } 
      if (Shutdown.isSHUTDOWN) {
        pc.sendPackets((ServerBasePacket)new S_SystemMessage("目前服务器准备关机状态"));
        return;
      } 
      if (pc.get_other().get_item_objid() != 0) {
        pc.sendPackets((ServerBasePacket)new S_SystemMessage("\\fT物品正在进行托售中,请在次重新操作一次"));
        pc.sendPackets((ServerBasePacket)new S_CloseList(pc.getId()));
        pc.get_other().set_item_objid(0);
        return;
      } 
      if (pc.get_followmaster() != null) {
        pc.sendPackets((ServerBasePacket)new S_SystemMessage("\\fT高宠状态中,无法交易。"));
        return;
      } 
      if (target.get_other().get_item_objid() != 0) {
        pc.sendPackets((ServerBasePacket)new S_SystemMessage("\\fT对方正在托售物品中请稍候"));
        pc.sendPackets((ServerBasePacket)new S_CloseList(pc.getId()));
        pc.get_other().set_item_objid(0);
        return;
      } 
      L1PcInstance srcTradetarget = (L1PcInstance)World.get().findObject(target.getTradeID());
      if (srcTradetarget != null) {
        L1Trade trade2 = new L1Trade();
        trade2.tradeCancel(srcTradetarget);
        return;
      } 
      if (target != null && !target.isParalyzed()) {
        pc.get_trade_clear();
        target.get_trade_clear();
        pc.setTradeID(target.getId());
        target.setTradeID(pc.getId());
        if (pc.hasSkillEffect(51234)) {
          target.sendPackets((ServerBasePacket)new S_Message_YN("尿尿商人"));
        } else {
          target.sendPackets((ServerBasePacket)new S_Message_YN(pc.getName()));
        } 
      } 
    } catch (Exception exception) {
    
    } finally {
      over();
    } 
  }
  
  public String getType() {
    return getClass().getSimpleName();
  }
}
